# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-admin-api
pkgver=1.16
pkgrel=0
pkgdesc="Backend for grommunio management"
# s390x: blocked by grommunio-gromox
# loongarch64: blocked by py3-jsonschema
arch="noarch !riscv64 !s390x !loongarch64"
url="https://grommunio.com/"
license="AGPL-3.0-only"
options="!check" # No test suite
subpackages="$pkgname-doc $pkgname-openrc $pkgname-bash-completion"
_php=83
makedepends="cmake py3-yaml samurai nginx"
depends="grommunio-admin-common
	grommunio-gromox
	py3-argcomplete
	py3-cryptography
	py3-dateutil
	py3-dnspython
	py3-flask
	py3-idna
	py3-jsonschema
	py3-jwt
	py3-ldap3
	py3-mattermostdriver
	py3-multidict
	py3-mysqlclient
	py3-openapi-core
	py3-psutil
	py3-pyexmdb
	py3-redis
	py3-requests
	py3-sqlalchemy
	py3-yaml
	uwsgi
	uwsgi-python3
	"

pkgusers="grommunio"
pkggroups="grommunio"

source="$pkgname-$pkgver.tar.gz::https://github.com/grommunio/admin-api/archive/refs/tags/$pkgver.tar.gz
	0001-makefile.patch
	0003-disable-systemd-log-reader.patch
	0004-replace-systemd-calls.patch
	0005-grommunio-antispam.patch

	grommunio-admin-api.initd
	grommunio-admin-api.confd
	database.yaml
	rc-service-ctl.sh
	"

builddir="$srcdir/admin-api-$pkgver"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DCMAKE_INSTALL_SBINDIR=/usr/sbin \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DCMAKE_INSTALL_LOCALSTATEDIR=/var \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DPHP_FPM_SERVICE="php-fpm$_php.service"

	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# create first dummy service to get the directory and overwrite it with symlink to uwsgi
	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	ln -sf "/etc/init.d/uwsgi" "$pkgdir/etc/init.d/$pkgname"
	install -m644 -D "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -d -m0750 -o grommunio -g grommunio "$pkgdir"/etc/grommunio-admin-api/conf.d
	install -d -m0700 -o grommunio -g grommunio "$pkgdir"/var/lib/grommunio-admin-api
	install -m644 -D "$srcdir"/database.yaml "$pkgdir"/etc/grommunio-admin-api/conf.d/database.yaml
	install -m755 -D "$srcdir"/rc-service-ctl.sh "$pkgdir"/usr/bin/rcctl
	install -d -m0775 -o grommunio -g nginx "$pkgdir"/var/run/grommunio
}

sha512sums="
e676eacd27e70a92259ca47fb5454a16ba1ddb0d9843d9d75fcbda5b65db11cb0fed853e7332c12ce562b4d5b3862dba1a45e34a1cc551d49a3238440b029a6e  grommunio-admin-api-1.16.tar.gz
cd4817590dae75eb728f3652f725b6dc4e9c998cadb174e4a2e9762ebea42f8aaa1cb7ebe9e346020f66937dd61dc8a6e7b07f88a70a5a550cfd0414835b0156  0001-makefile.patch
27dceea394302b2afafca84927c3fc9a57cb6502e4fd08ccd35786855c6bd3203869a357d89766e3775c4c05882105372ab6c9db5b974fc727fa8b4753387236  0003-disable-systemd-log-reader.patch
815e3151c10be79b9471c28f871af6f80afcf1cbd753091d141d2e66b7c0f32d58e2ca6f0967168c8efb6cdb6126626904384f09983f63f8a17fd80abdadab52  0004-replace-systemd-calls.patch
afa5f48dc086aef4b43dd6b0afe656ee9f7c1d501ef326e003f6df150c2c478e2ba035e10c30cec790f67663557144a7186a37e4499e50933174df64ff880480  0005-grommunio-antispam.patch
b87910a731ad58b86679b08674632fffb4d76a94ad67602906200acf7e36fb23508ec19f56ca2f8a1e257a7ef3a32c4dbfa7b27d9d902006eff3d92d8816c067  grommunio-admin-api.initd
5c579c7421348839be0bb8db4ab9047e7882589e9a674d81bdd5685821ae836bbe4c6a15e478bd68977435da95b5a66427547e6d282f576f106944bffb94c328  grommunio-admin-api.confd
bf166f9f46d3a6755c2273502576c63060b17d2eb7a4abbac9da5ed1b74a4ed47baf6269eda58adf04657a78028e862572cd7b24d9c8b5b6497b778338def12f  database.yaml
909a211694af56277b167ff2f68a9cacf0f09b1f78364405012e1139bf3b7a931d17f971ed30233eca3e9a1039d98ec32e35f6b52699387f81016819b69468c5  rc-service-ctl.sh
"
